//
// C++ Implementation: qtactivityview
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "activityviewwidget.h"
#include <QDebug>

/**
* Constructor.
*/
ActivityViewWidget::ActivityViewWidget ( QWidget *parent )
        : QWidget ( parent )
{
    activity = new Activity;
    ui.setupUi ( this );

    connect(ui.cb_chooseDate, SIGNAL(activated(int)), this, SLOT(showActivity(int)));
    connect(ui.pb_later, SIGNAL(clicked()), this, SLOT(nextActivity()));
    connect(ui.pb_earlier, SIGNAL(clicked()), this, SLOT(prevActivity()));
    connect(ui.pb_edit, SIGNAL(clicked()), this, SLOT(getEdit()));
    connect(ui.pb_new, SIGNAL(clicked()), this, SLOT(newActivity()));
}

/**
* Constructor.
* @param activities A reference to the structure of activities that can be viewed.
*/
ActivityViewWidget::ActivityViewWidget ( QVector<Activity>* activities, QWidget *parent )
        : QWidget ( parent )
{
    activity = new Activity;

    ui.setupUi ( this );

    connect(ui.cb_chooseDate, SIGNAL(activated(int)), this, SLOT(showActivity(int)));
    connect(ui.pb_later, SIGNAL(clicked()), this, SLOT(nextActivity()));
    connect(ui.pb_earlier, SIGNAL(clicked()), this, SLOT(prevActivity()));
    connect(ui.pb_edit, SIGNAL(clicked()), this, SLOT(editActivity()));
    connect(ui.pb_new, SIGNAL(clicked()), this, SLOT(newActivity()));

    setActivities(activities);
}

/**
* Destructor.
*/
ActivityViewWidget::~ActivityViewWidget()
{
}

/**
* Updates the vector of activities to be displayed in this widget.
* @param vector A vector of activities to be display-able.
*/
void ActivityViewWidget::setActivities(QVector<Activity>* vector)
{
    index = 0;
    this->activities = vector;

    *activity = activities->value(index);

    updateView();
}

/**
* Checks to see if the current activity is on an extreme end
* of the vector, and if so, disable the respective fore/back
* button.
*/
void ActivityViewWidget::checkButtons()
{
    if ( index <= 0 )
    {
        ui.pb_earlier->setEnabled ( false );
    }
    else
    {
        ui.pb_earlier->setEnabled ( true );
    }

    if (index + 1  >=  activities->size())
    {
        ui.pb_later->setEnabled ( false );
    }
    else
    {
        ui.pb_later->setEnabled ( true );
    }
}


/**
* Updates the displayed data regarding an activity.
*/
void ActivityViewWidget::updateView()
{
    int i;
    ui.cb_chooseDate->clear();
    for( i = 0; i < activities->size(); i++)
    {
        ui.cb_chooseDate->addItem(activities->value(i).getDate().toString(), i);
    }

    ui.le_date->setText(activity->getDate().toString());
    ui.le_timeOfAct->setText(activity->getTime().toString());
    ui.le_time->setText(activity->getDuration().toString("hh:mm:ss"));
    ui.le_distance->setText(QString::number(activity->getDistance()));
    ui.le_effort->setText(QString(activity->getEffort()));
    ui.le_type->setText(activity->getType());
    ui.le_location->setText(activity->getLocation());
    ui.le_route->setText(activity->getRoute());
    ui.le_notes->setText(activity->getNotes());
    ui.le_splits->setText(activity->getSplits());


//TODO:
//  this can be a problem if activities are added to the model,
// esp before the index, but the index doesnt change.
// do a lookup in the combo box
    ui.cb_chooseDate->setCurrentIndex(index);

    checkButtons();
}

/**
* Changes the view to the next available activity.
*/
void ActivityViewWidget::nextActivity()
{
    if (index + 1  >=  activities->size())
    {
        // do nothing
    }
    else
    {
        index++;
        *activity = activities->value(index);
    }
    updateView();
}

/**
* Changes the view to the previous activity.
*/
void ActivityViewWidget::prevActivity()
{
    if ( index <= 0 )
    {
        // do nothing
    }
    else
    {
        index--;
        *activity = activities->value(index);
    }
    updateView();
}

/**
* Selects and shows the activity that took place on a date.
* @param date The date on which the activity took place.
* TODO: distinguish among multiple activities on a date, or none at all.
*/
void ActivityViewWidget::showActivity(QDate date)
{
    int i;
    for(i = 0; i < activities->size(); i++)
    {
        if(activities->value(i).getDate() == date)
        {
            *activity = activities->value(i);
            index = i;
        }
    }
    updateView();
}


/**
* Overloaded function, selects activity by index in vector.
* @param index The index in the vector of activities to show.
*/
void ActivityViewWidget::showActivity(int index)
{
    if( index < activities->size() )
    {
        *activity = activities->value(index);
        this->index = index;
    }
    updateView();
    
}

/**
* This function emits a signal that an activity needs to be edited.
* It passes a reference to the activity to be edited as a parameter to
* the signal.
* TODO:
* Problems might arise if the vector is updated while an activity is
* being edited.
*/
void ActivityViewWidget::editActivity()
{
    Activity* actArray = activities->data();
    Activity* actToEdit = &(actArray[index]);

    emit(needEdit(actToEdit));
}

void ActivityViewWidget::newActivity()
{
    Activity newActivity;
    activities->append(newActivity);

    Activity* actArray = activities->data();
    Activity* actToEdit = &(actArray[activities->size() - 1]);

    emit(needEdit(actToEdit));
}


